//
//  BCNSpcFormula.m
//  Beacon
//
//  Created by arden on 12/3/11.
//  Copyright 2011 Fried Chicken Legs. All rights reserved.
//


#import "BCNSpcFormula.h"
#import <math.h>

static double d2 = 1.128;

@implementation BCNSpcFormula

+(double) Sigma : (BCNSpcChartType) type 
        	Xbar:(double) Xbar 
        	Range:(double) Range 
        	Size:(double) sampleSize
        {
            switch (type)
            {
                case BCNSpcChartType.XBarR:
                    return BCNSpcConstant.GetA2s((int)sampleSize) * Range / 3.0;
                    
                case BCNSpcChartType.XBarS:
                    return sampleSize <= 25 ? BCNSpcConstant.GetCns((int)sampleSize) * Range / sqrt(sampleSize) 
                        : Range / BCNSpcConstant.GetC4s((int)sampleSize) / sqrt(sampleSize);
                    
                case BCNSpcChartType.XMR:
                    return Range / d2;
                    
                case BCNSpcChartType.C:
                    return sqrt(Xbar);
                    
                case BCNSpcChartType.U:
                    return sqrt(Xbar / sampleSize);
                    
                case BCNSpcChartType.P:
                    return sqrt(Xbar * (1 - Xbar) / sampleSize);
                    
                case BCNSpcChartType.NP:
                    return sqrt(Xbar * (1 - Xbar / sampleSize));
                default:
                    return MAXFLOAT;
            }
        }
        
@end // BCNSpcFormula	

